﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcVehicleInsurance.Models;
using System.ComponentModel.DataAnnotations;

namespace MvcVehicleInsurance.Areas.Admin.Controllers
{
   
    public class EstimateController : Controller
    {
        private DataClassesVehicleDataContext data = new DataClassesVehicleDataContext();

        //
        // GET: /Estimate/

        public ActionResult Index()
        {

            List<Estimate> es = (from e in data.Estimates select e).ToList();
            if (ModelState.IsValid)
            {
                return View(es);

            }

            return View();
        }

        //
        // GET: /Estimate/Details/5

        public ActionResult Details(int id)
        {
            Estimate es = (data.Estimates.Where(e => e.estimateNumber == id)).First();
            if (ModelState.IsValid)
            {
                return View(es);
            }
            return View();
        }





        //
        // GET: /Estimate/Add
        public ActionResult Add()
        {
            List<CustomerRecord> listcustomer = data.CustomerRecords.ToList();
            ViewData["listcustomer"] = new SelectList(listcustomer, "customerID", "customerFullName");
            return View();
        }

        [HttpPost]
        public ActionResult Add(CustomerRecord cust)
        {
            List<CustomerRecord> listcustomer = data.CustomerRecords.ToList();
            ViewData["listcustomer"] = new SelectList(listcustomer, "customerID", "customerFullName");
            Session["SelectCustID"] = cust.customerID;
            Response.Write(Session["SelectCustID"]);
            Response.Redirect("/Admin/Estimate/Create");
            return View();
        }


        //GET: /Estimate/Create/
        public ActionResult Create()
        {
            if (Session["SelectCustID"] != null)
            {
                int custID = Int32.Parse(Session["SelectCustID"].ToString());
                List<CustomerBillingInformation> listbill = (from bill in data.CustomerBillingInformations.Where(b => b.customerID == custID) select bill).ToList();
                ViewData["listBill"] = new SelectList(listbill, "billNo", "date");
            }
            List<VehicleInformation> listVehicle = (from Vehicle in data.VehicleInformations select Vehicle).ToList();
            List<CustomerPolicyRecord> listPolicy = (from Policy in data.CustomerPolicyRecords select Policy).ToList();
            ViewData["listVehicle"] = new SelectList(listVehicle, "vehicleID", "vehicleName");
            ViewData["listPolicy"] = new SelectList(listPolicy, "policyNumber", "policyDuration");
            Estimate e = new Estimate();
            return View(e);
        }

        //
        // POST: /Estimate/Create

        [HttpPost]
        public ActionResult Create(Estimate e)
        {
            List<CustomerBillingInformation> listbill = (from bill in data.CustomerBillingInformations select bill).ToList();
            List<VehicleInformation> listVehicle = (from Vehicle in data.VehicleInformations select Vehicle).ToList();
            List<CustomerPolicyRecord> listPolicy = (from Policy in data.CustomerPolicyRecords select Policy).ToList();

            ViewData["listBill"] = new SelectList(listbill, "billNo", "date");
            ViewData["listVehicle"] = new SelectList(listVehicle, "vehicleID", "vehicleName");
            ViewData["listPolicy"] = new SelectList(listPolicy, "policyNumber", "policyDuration");

            if (ModelState.IsValid)
            {
                data.Estimates.InsertOnSubmit(e);
                data.SubmitChanges();
                RedirectToAction("Index");
            }
            return View();

        }





        //
        // GET: /Estimate/Edit/5

        public ActionResult Edit(int id)
        {
            Estimate es = new Estimate();
            return View();
        }

        //
        // POST: /Estimate/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, Estimate es)
        {
            if (ModelState.IsValid)
            {
                Estimate e = data.Estimates.Where(est => est.estimateNumber == id).First();
                e.vehicleID = es.vehicleID;
                e.policyNumber = es.policyNumber;
                e.billNo = es.billNo;
                RedirectToAction("Index");
            }

            return View();

        }


    }
}
